//
//  StationTableViewCell.m
//  ICrEAM
//
//  Created by Leona on 2017/4/18.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "StationTableViewCell.h"

@implementation StationTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark    初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *repairEventCellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-ActualWidth(20), ActualHeight(25))];
        repairEventCellView.backgroundColor = BackgroundColor_Gray;
        [self.contentView addSubview:repairEventCellView];
        _operateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _operateButton.frame = CGRectMake(0, ActualHeight(5), ActualWidth(50), ActualHeight(25));
        [_operateButton addTarget:self action:@selector(clickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
        
        _icon = [[UIImageView alloc] initWithFrame:CGRectMake(ActualWidth(17.5), ActualHeight(5), ActualWidth(15), ActualHeight(15))];
        _icon.image = [UIImage imageNamed:@"btn_tableview_delete"];
        
        [repairEventCellView addSubview:_icon];
        [repairEventCellView addSubview:_operateButton];
        
        
        
        
        UIView *firstSep = [[UIView alloc] initWithFrame:CGRectMake(_operateButton.rightEdge, 0, 1, repairEventCellView.height)];
        firstSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:firstSep];
        
        UILabel *orderLabel = [self createLabelWithText:@"" frame:CGRectMake(firstSep.rightEdge, _operateButton.y, ActualWidth(50), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
        [repairEventCellView addSubview:orderLabel];
        self.orderLabel = orderLabel;
        
        UIView *secondSep = [[UIView alloc] initWithFrame:CGRectMake(orderLabel.rightEdge, 0, 1, repairEventCellView.height)];
        secondSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:secondSep];
        
        UILabel *eventNumberLabel = [self createLabelWithText:@"" frame:CGRectMake(secondSep.rightEdge, _operateButton.y, ActualWidth(145), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
        [repairEventCellView addSubview:eventNumberLabel];
        self.eventNumberLabel = eventNumberLabel;
        
        UIView *thirdSep = [[UIView alloc] initWithFrame:CGRectMake(eventNumberLabel.rightEdge, 0, 1, repairEventCellView.height)];
        thirdSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:thirdSep];
        
        UILabel *eventNameLabel = [self createLabelWithText:@"" frame:CGRectMake(thirdSep.rightEdge, _operateButton.y, ActualWidth(215), ActualHeight(15)) textAlignment:NSTextAlignmentLeft];
        [repairEventCellView addSubview:eventNameLabel];
        self.eventNameLabel = eventNameLabel;
        
        UIView *fourthSep = [[UIView alloc] initWithFrame:CGRectMake(eventNameLabel.rightEdge, 0, 1, repairEventCellView.height)];
        fourthSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:fourthSep];
        
        //        UILabel *workingTimeLabel = [self createLabelWithText:@"" frame:CGRectMake(fourthSep.rightEdge, operateButton.y, ActualWidth(75), ActualHeight(15)) textAlignment:NSTextAlignmentRight];
        UITextField *workingTimeLabel = [[UITextField alloc] initWithFrame:CGRectMake(fourthSep.rightEdge, _operateButton.y, ActualWidth(75), ActualHeight(15))];
        workingTimeLabel.delegate=self;
        workingTimeLabel.text = @"";
        workingTimeLabel.font = ContentFont;
        workingTimeLabel.textColor = ContentTextColor_Black;
        workingTimeLabel.textAlignment = NSTextAlignmentRight;
        [repairEventCellView addSubview:workingTimeLabel];
        self.workingTimeLabel = workingTimeLabel;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(collectionDidChange:) name:UITextFieldTextDidChangeNotification object:self.workingTimeLabel];
        
        UIView *fifthSep = [[UIView alloc] initWithFrame:CGRectMake(workingTimeLabel.rightEdge, 0, 1, repairEventCellView.height)];
        fifthSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:fifthSep];
        
        UILabel *priceLabel = [self createLabelWithText:@"" frame:CGRectMake(fifthSep.rightEdge, _operateButton.y, ActualWidth(75), ActualHeight(15)) textAlignment:NSTextAlignmentRight];
        [repairEventCellView addSubview:priceLabel];
        self.priceLabel = priceLabel;
        
        UIView *addSep = [[UIView alloc] initWithFrame:CGRectMake(priceLabel.rightEdge, 0, 1, repairEventCellView.height)];
        addSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:addSep];
        
        UILabel *workingPriceLabel = [self createLabelWithText:@"" frame:CGRectMake(addSep.rightEdge, _operateButton.y, ActualWidth(75), ActualHeight(15)) textAlignment:NSTextAlignmentRight];
        [repairEventCellView addSubview:workingPriceLabel];
        self.workingPriceLabel = workingPriceLabel;
        
        UIView *sixthSep = [[UIView alloc] initWithFrame:CGRectMake(workingPriceLabel.rightEdge, 0, 1, repairEventCellView.height)];
        sixthSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:sixthSep];
        
        
        UILabel *repairTypeLabel = [self createLabelWithText:@"" frame:CGRectMake(sixthSep.rightEdge, _operateButton.y, ActualWidth(75), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
        repairTypeLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRepairType)];
        [repairTypeLabel addGestureRecognizer:tap];
        [repairEventCellView addSubview:repairTypeLabel];
        self.repairTypeLabel = repairTypeLabel;
        
        UIView *seventhSep = [[UIView alloc] initWithFrame:CGRectMake(repairTypeLabel.rightEdge, 0, 1, repairEventCellView.height)];
        seventhSep.backgroundColor = BorderColor_Gray;
        [repairEventCellView addSubview:seventhSep];
        
        UITextField *breakdownDiscribeTextField = [[UITextField alloc] initWithFrame:CGRectMake(seventhSep.rightEdge, _operateButton.y, ScreenWidth-ActualWidth(40)-repairTypeLabel.rightEdge, ActualHeight(15))];
        breakdownDiscribeTextField.delegate = self;
        [repairEventCellView addSubview:breakdownDiscribeTextField];
        self.breakdownDiscribeTextField = breakdownDiscribeTextField;
    }
    return self;
}
- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame textAlignment:(NSTextAlignment)textAlignment{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = ContentFont;
    label.textColor = ContentTextColor_Black;
    label.textAlignment = textAlignment;
    return label;
}
-(NSString *)calculateWorkingHoursWithSERIES_LABOUR_PRICE:(NSString *)SERIES_LABOUR_PRICE andREPAIR_ITEM_TYPE_RATE:(NSString *)REPAIR_ITEM_TYPE_RATE andTECHNIC_MODULUS:(NSString *)TECHNIC_MODULUS{
    
    double series_labour_price = [SERIES_LABOUR_PRICE floatValue];
    double repair_item_type_rate = [REPAIR_ITEM_TYPE_RATE floatValue];
    double texhnic_modulus = [TECHNIC_MODULUS floatValue];
    
    double workingHours = series_labour_price * repair_item_type_rate * texhnic_modulus;
    
    NSString *workingHoursStr = [NSString stringWithFormat:@"%.2f",workingHours];
    
    return workingHoursStr;
}

#pragma mark    根据model设置界面
- (void)setupLabelsWithModel:(ICMRepairMaintenanceModel *)model{
    self.model = model;
    if ([model.LABOUR_HOUR_EDIT isEqualToString:@"12781002"]) {
        self.workingTimeLabel.enabled=NO;
    }else{
        self.workingTimeLabel.enabled=YES;
    }
    
    self.orderLabel.text = [@(self.order+1) stringValue];
    self.eventNameLabel.text = model.LABOUR_NAME;
    self.eventNumberLabel.text = model.LABOUR_CODE;
    self.workingTimeLabel.text = model.STD_LABOUR_HOUR;
    if([model.LABOUR_AMOUNT isKindOfClass:[NSString class]]){
        self.workingPriceLabel.text = [NSString stringWithFormat:@"%.2f",[model.LABOUR_AMOUNT floatValue]];
    }else{
        self.workingPriceLabel.text = @"0.00";
    }
//    self.priceLabel.text = [NSString stringWithFormat:@"%.2f",[model.LABOUR_PRICE floatValue]];
    self.repairTypeLabel.text = model.REPAIR_TYPE_NAME;
    self.breakdownDiscribeTextField.text = model.TROUBLE_DESC;
    self.priceLabel.text = [self calculateWorkingHoursWithSERIES_LABOUR_PRICE:self.SERIES_LABOUR_PRICE andREPAIR_ITEM_TYPE_RATE:model.REPAIR_ITEM_TYPE_RATE andTECHNIC_MODULUS:model.TECHNIC_MODULUS];

}

@end
